package wemai;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class m_21cncom extends WebMail {

	public void send(String uid, String password, String to, String subject, String content) throws Exception {
		to = to.replaceAll(";", ",");

		try {
			String url = "http://mail.21cn.com/";
			dr.get(url);

			dr.findElement(By.name("UserName")).sendKeys(uid);
			dr.findElement(By.name("passwd")).sendKeys(password);
			dr.findElement(By.name("Submit")).click();

			waitWebElement(By.id("navFrame"));

			dr.switchTo().frame("navFrame");

			waitWebElement(By.linkText("写信")).click();

			dr.switchTo().defaultContent().switchTo().frame("mainFrame");

			waitWebElement(By.className("NavLeft"));

			dr.findElement(By.id("arrowRecentContact")).click();

			dr.findElement(By.name("subject")).sendKeys(subject);

			dr.findElement(By.name("saveSended")).click();

			content = StringUtils.remove(content, '\r');
			content = StringUtils.remove(content, '\n');

			content = StringEscapeUtils.escapeJavaScript(content);

			String js = "$(window.frames['contentHtml'].frames['Composition'].document).find('body').html('" + content + "')";
			((JavascriptExecutor) dr).executeScript(js);

			sleep(2000);
			dr.findElement(By.name("to")).sendKeys(to);
			sleep(2000);
			dr.findElement(By.name("saveSended")).click();

			js = "$(window.frames['contentHtml'].frames['Composition'].document).find('body').html('" + content + "')";
			((JavascriptExecutor) dr).executeScript(js);

			((JavascriptExecutor) dr).executeScript(" immsend(); ");

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			sleep(6000);
			dr.quit();
		}
	}

	public static void main(String[] args) {
		try {
			String uid = "wewinno1";
			String password = "qq3131798654";
			String to = "o21x@qq.com";
			String subject = "图片频道十分钟告警";
			String content = "0000000000000000000000000";

			new m_21cncom().send(uid, password, to, subject, content);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
